#include "stdafx.h"
#include <shlwapi.h>
#include <shlobj.h>
#include "ServerConfig.h"
#include "../../../Components/Common/LibCommon/util.h"
#include "../../../ThirdParty/tinyxml-2.5.3/tinyxml.h"

#define END_BY_NULL(x) if (!x) return false;

CServerConfig::CServerConfig()
{
	m_Port = 8080;
	m_bAutoLogin = false;
	m_bAutoStartup = false;
	CreateConfigDirectory();
}

void  CServerConfig::CreateConfigDirectory()
{
	TCHAR szDefaultPath[MAX_PATH];
	TCHAR szBasePath[MAX_PATH];

	GetModuleFileName(NULL, szDefaultPath, MAX_PATH);
	PathRemoveFileSpec(szDefaultPath);
	if(szDefaultPath[_tcslen(szDefaultPath) - 1] != '\\')
		_tcscat(szDefaultPath, _T("\\"));

	if(SUCCEEDED(SHGetFolderPath(NULL,CSIDL_APPDATA|CSIDL_FLAG_CREATE,	NULL, 0, szBasePath))) 
	{
		if(szBasePath[_tcslen(szBasePath) - 1] != '\\')
			_tcscat(szBasePath, _T("\\"));
		
		m_ConfigPath = szBasePath;
		m_ConfigPath += _T("DocoMedia\\WinHomeServer\\");
		if (!CreateFolder(m_ConfigPath))
			m_ConfigPath = szDefaultPath;
	}
}

bool CServerConfig::SaveConfig()
{
	char convbuf[1024];
	string cfg_path = WideCharToMultiChar(m_ConfigPath);
	cfg_path += SERVER_CONFIG_FILENAME;
	TiXmlDocument XmlDoc(cfg_path.c_str());
	TiXmlDeclaration * decl = new TiXmlDeclaration( "1.0", "utf-8", "yes" );
	TiXmlElement * root = new TiXmlElement("HomeMediaServerCfg");

	TiXmlElement * port = new TiXmlElement("Port");
	TiXmlText * port_text = new TiXmlText(itoa(m_Port, convbuf, 10));
	port->LinkEndChild(port_text);

	TiXmlElement * user = new TiXmlElement("User");
	TiXmlText * user_text = new TiXmlText(WideCharToUTF8(m_Username).c_str());
	user->LinkEndChild(user_text);

	TiXmlElement * token = new TiXmlElement("Token");
	TiXmlText * token_text = new TiXmlText(WideCharToUTF8(m_Token).c_str());
	token->LinkEndChild(token_text);

	TiXmlElement * AutoLogin = new TiXmlElement("AutoLogin");
	TiXmlText * AutoLogin_text = new TiXmlText(m_bAutoLogin?"true":"false");
	AutoLogin->LinkEndChild(AutoLogin_text);

	TiXmlElement * AutoStartup = new TiXmlElement("AutoStartup");
	TiXmlText * AutoStartup_text = new TiXmlText(m_bAutoStartup?"true":"false");
	AutoStartup->LinkEndChild(AutoStartup_text);

	TiXmlElement * ShareFolders = new TiXmlElement("ShareFolders");
	for (unsigned int i = 0; i < m_ShareFolders.size(); i++)
	{
		TiXmlElement * Item = new TiXmlElement("Item");
		TiXmlText * Item_text = new TiXmlText(WideCharToUTF8(m_ShareFolders[i]).c_str());
		Item->LinkEndChild(Item_text);
		ShareFolders->LinkEndChild(Item);
	}

	XmlDoc.LinkEndChild(decl);
	XmlDoc.LinkEndChild(root);
	root->LinkEndChild(port);
	root->LinkEndChild(user);
	root->LinkEndChild(token);
	root->LinkEndChild(AutoLogin);
	root->LinkEndChild(AutoStartup);
	root->LinkEndChild(ShareFolders);
	
	return XmlDoc.SaveFile();
}

bool CServerConfig::LoadConfig()
{
	string cfg_path = WideCharToMultiChar(m_ConfigPath);
	cfg_path += SERVER_CONFIG_FILENAME;
	TiXmlDocument XmlDoc(cfg_path.c_str());

	if (!XmlDoc.LoadFile())
		return false;

	TiXmlNode * root = XmlDoc.FirstChild("HomeMediaServerCfg");
	END_BY_NULL(root);

	TiXmlElement * port = (TiXmlElement*)root->FirstChild("Port");
	END_BY_NULL(port);
	TiXmlText * port_text = (TiXmlText *)port->FirstChild();
	END_BY_NULL(port_text);
	m_Port = atoi(port_text->Value());

	TiXmlElement * user = (TiXmlElement*)root->FirstChild("User"); 
	END_BY_NULL(user);
	TiXmlText * user_text =  (TiXmlText *)user->FirstChild();
	if (user_text)
		m_Username = UTF8ToWideChar(string(user_text->Value()));

	TiXmlElement * token = (TiXmlElement*)root->FirstChild("Token");
	END_BY_NULL(token);
	TiXmlText * token_text = (TiXmlText *)token->FirstChild();
	if (token_text)
		m_Token = UTF8ToWideChar(string(token_text->Value()));

	TiXmlElement * AutoLogin = (TiXmlElement*)root->FirstChild("AutoLogin");
	END_BY_NULL(AutoLogin);
	TiXmlText * AutoLogin_text = (TiXmlText *)AutoLogin->FirstChild();
	END_BY_NULL(AutoLogin_text);
	m_bAutoLogin = (strcmp(AutoLogin_text->Value(), "true") == 0);

	TiXmlElement * AutoStartup = (TiXmlElement*)root->FirstChild("AutoStartup");
	END_BY_NULL(AutoStartup);
	TiXmlText * AutoStartup_text = (TiXmlText *)AutoStartup->FirstChild();
	END_BY_NULL(AutoStartup_text);
	m_bAutoStartup = (strcmp(AutoStartup_text->Value(), "true") == 0);

	TiXmlElement * ShareFolders = (TiXmlElement*)root->FirstChild("ShareFolders");
	END_BY_NULL(ShareFolders);
	TiXmlElement * Item = (TiXmlElement*)ShareFolders->FirstChild("Item");
	while (Item != NULL)
	{
		TiXmlText * Item_text = (TiXmlText *)Item->FirstChild();
		if (Item_text)
			m_ShareFolders.push_back(UTF8ToWideChar(string(Item_text->Value())));
		Item = Item->NextSiblingElement();
	}
	return true;
}

bool CServerConfig::AddShareFolder(wstring folder)
{
	deque<wstring>::iterator it;
	for (it = m_ShareFolders.begin(); it != m_ShareFolders.end(); it++)
	{
		if (*it == folder)
			return true;
	}
	m_ShareFolders.push_back(folder);
	return true;
}

bool CServerConfig::RemoveShareFolder(wstring folder)
{
	deque<wstring>::iterator it;
	for (it = m_ShareFolders.begin(); it != m_ShareFolders.end(); it++)
	{
		if (*it == folder)
		{
			m_ShareFolders.erase(it);
			return true;
		}
	}
	return false;
}
